<template>
  <a-card :bordered="false">
    <content-header title="行业列表"></content-header>
    <div class="table-operator">
      <a-button type="primary" icon="plus" @click="$refs.createModal.show()">新增</a-button>
    </div>
    <s-table
      ref="table"
      size="default"
      rowKey="id"
      :columns="columns"
      :data="loadData"
      showPagination="auto"
      :pageSize="15"
    >
      <span slot="action" slot-scope="text, item">
        <template>
          <a @click="handleEdit(item)">配置</a>
          <a-divider type="vertical" />
          <a @click="handleDelete(item)">删除</a>
        </template>
      </span>
    </s-table>
    <create-industry-form ref="createModal" @handleSubmit="handleRefresh" />
    <edit-industry-form ref="editModal" @handleSubmit="handleRefresh" />
  </a-card>
</template>

<script>
import * as Api from '@/api/industry/api'
import { ContentHeader, STable } from '@/components'
import CreateIndustryForm from '@/views/store/modules/CreateIndustryForm'
import EditIndustryForm from '@/views/store/modules/EditIndustryForm'

export default {
  name: 'Industry',
  components: {
    ContentHeader,
    STable,
    CreateIndustryForm,
    EditIndustryForm
  },
  data () {
    return {
      // 查询参数
      queryParam: {},
      // 表头
      columns: [
        {
          title: '行业ID',
          dataIndex: 'id'
        },
        {
          title: '行业名称',
          dataIndex: 'name'
        },
        {
          title: '状态',
          dataIndex: 'status',
          customRender: (value) => {
            let response = ''
            switch (value) {
              case 0:
                response = '关闭'
                break

              case 1:
                response = '开启'
                break
            }
            return response
          }
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '150px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: param => {
        return Api.list(Object.assign(param, this.queryParam))
          .then(response => {
            return response.data.list
          })
      }
    }
  },
  created () {
  },
  methods: {
    /**
     * 删除记录
     */
    handleDelete (item) {
      const self = this
      this.$confirm({
        title: '您确定要删除该记录吗?',
        content: '确认后将删除当前分类',
        // okType: 'danger',
        onOk () {
          // 确认删除
          return self.onSubmitDelete(item)
        }
      })
    },

    /**
     * 确认删除
     */
    onSubmitDelete (item) {
      return Api.deleted({ id: item['id'] })
        .then((result) => {
          this.$message.success(result.message)
          this.handleRefresh()
        })
    },

    /**
     * 编辑记录
     */
    handleEdit (item) {
      this.$refs.editModal.edit(item)
    },

    /**
     * 刷新列表
     */
    handleRefresh () {
      this.$refs.table.refresh()
    }
  }
}
</script>
